﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTSandInspection : BasePage
    {
        string wholSequence = "LT/S实-12-01";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method

        private void BindGridView()
        {
            RPTSandInspectionService RPTSandInspectionServiceObj = ServiceFactory.getRPTSandInspectionService();
            OperationResult<List<tb_RPT_SandInspection>> result = RPTSandInspectionServiceObj.GetAllSandInspection();

            if (result.operationSuccess)
            {
                gvRPTSandInspectionList.DataSource = result.obj;
                gvRPTSandInspectionList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTSandInspectionService RPTSandInspectionServiceObj = ServiceFactory.getRPTSandInspectionService();
            OperationResult<tb_RPT_SandInspection> resultEdit = RPTSandInspectionServiceObj.GetSandInspectionById(id);

            if (resultEdit.operationSuccess == true)
            {
                txtEditProjectName.Text = resultEdit.obj.ProjectName;
                txtEditMaterial.Text = resultEdit.obj.Material;
                txtEditTestDate.Text = resultEdit.obj.TestDate;
                txtEditSiltContent.Text = resultEdit.obj.SiltContent;
                txtEditStonePowderContent.Text = resultEdit.obj.StonePowderContent;
                txtEditClodContent.Text = resultEdit.obj.ClodContent;
                txtEditHarmfulMaterial.Text = resultEdit.obj.HarmfulMaterial;
                txtEditConsistence.Text = resultEdit.obj.Consistence;
                txtEditCrushingIndex.Text = resultEdit.obj.CrushingIndex;
                txtEditApparentDensity.Text = resultEdit.obj.ApparentDensity;
                txtEditStackingDensity.Text = resultEdit.obj.StackingDensity;
                txtEditVoids.Text = resultEdit.obj.Voids;
                txtEditJianJiLiaoReaction.Text = resultEdit.obj.JianJiLiaoReaction;
                txtEditAllowance1.Text = resultEdit.obj.Allowance1;
                txtEditAllowance2.Text = resultEdit.obj.Allowance2;
                txtEditAllowance3.Text = resultEdit.obj.Allowance3;
                txtEditAllowance4.Text = resultEdit.obj.Allowance4;
                txtEditAllowance5.Text = resultEdit.obj.Allowance5;
                txtEditAllowance6.Text = resultEdit.obj.Allowance6;
                txtEditAllowance7.Text = resultEdit.obj.Allowance7;
                txtEditTestStandard.Text = resultEdit.obj.TestStandard;
                txtEditTestResult.Text = resultEdit.obj.TestResult;
                txtEditRemark.Text = resultEdit.obj.Remark;
                txtEditReview.Text = resultEdit.obj.Review;
                txtEditApproval.Text = resultEdit.obj.Approval;
                txtEditTest.Text = resultEdit.obj.Test;


                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTSandInspectionService RPTSandInspectionServiceObj = ServiceFactory.getRPTSandInspectionService();
            tb_RPT_SandInspection instance = new tb_RPT_SandInspection();
            instance.Sequence = wholSequence; 
            instance.ProjectName = txtProjectName.Text;
            instance.Material = txtMaterial.Text;
            instance.TestDate = txtTestDate.Text;
            instance.SiltContent = txtSiltContent.Text;
            instance.StonePowderContent = txtStonePowderContent.Text;
            instance.ClodContent = txtClodContent.Text;
            instance.HarmfulMaterial = txtHarmfulMaterial.Text;
            instance.Consistence = txtConsistence.Text;
            instance.CrushingIndex = txtCrushingIndex.Text;
            instance.ApparentDensity = txtApparentDensity.Text;
            instance.StackingDensity = txtStackingDensity.Text;
            instance.Voids = txtVoids.Text;
            instance.JianJiLiaoReaction = txtJianJiLiaoReaction.Text;
            instance.Allowance1 = txtAllowance1.Text;
            instance.Allowance2 = txtAllowance2.Text;
            instance.Allowance3 = txtAllowance3.Text;
            instance.Allowance4 = txtAllowance4.Text;
            instance.Allowance5 = txtAllowance5.Text;
            instance.Allowance6 = txtAllowance6.Text;
            instance.Allowance7 = txtAllowance7.Text;
            instance.TestStandard = txtTestStandard.Text;
            instance.TestResult = txtTestResult.Text;
            instance.Remark = txtRemark.Text;
            instance.Review = txtReview.Text;
            instance.Approval = txtApproval.Text;
            instance.Test = txtTest.Text; 

            OperationResult<tb_RPT_SandInspection> result = RPTSandInspectionServiceObj.CreateSandInspection(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTSandInspectionService RPTSandInspectionServiceObj = ServiceFactory.getRPTSandInspectionService();
            tb_RPT_SandInspection entity = new tb_RPT_SandInspection();

            entity.Id = editId;
            entity.Sequence = wholSequence;
            entity.ProjectName = txtEditProjectName.Text;
            entity.Material = txtEditMaterial.Text;
            entity.TestDate = txtEditTestDate.Text;
            entity.SiltContent = txtEditSiltContent.Text;
            entity.StonePowderContent = txtEditStonePowderContent.Text;
            entity.ClodContent = txtEditClodContent.Text;
            entity.HarmfulMaterial = txtEditHarmfulMaterial.Text;
            entity.Consistence = txtEditConsistence.Text;
            entity.CrushingIndex = txtEditCrushingIndex.Text;
            entity.ApparentDensity = txtEditApparentDensity.Text;
            entity.StackingDensity = txtEditStackingDensity.Text;
            entity.Voids = txtEditVoids.Text;
            entity.JianJiLiaoReaction = txtEditJianJiLiaoReaction.Text;
            entity.Allowance1 = txtEditAllowance1.Text;
            entity.Allowance2 = txtEditAllowance2.Text;
            entity.Allowance3 = txtEditAllowance3.Text;
            entity.Allowance4 = txtEditAllowance4.Text;
            entity.Allowance5 = txtEditAllowance5.Text;
            entity.Allowance6 = txtEditAllowance6.Text;
            entity.Allowance7 = txtEditAllowance7.Text;
            entity.TestStandard = txtEditTestStandard.Text;
            entity.TestResult = txtEditTestResult.Text;
            entity.Remark = txtEditRemark.Text;
            entity.Review = txtEditReview.Text;
            entity.Approval = txtEditApproval.Text;
            entity.Test = txtEditTest.Text;  

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_SandInspection> resultEdit = RPTSandInspectionServiceObj.ModifySandInspection(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTSandInspectionService RPTSandInspectionServiceObj = ServiceFactory.getRPTSandInspectionService();
            tb_RPT_SandInspection deleteEntity = new tb_RPT_SandInspection();

            deleteEntity.Id = deleteId;
            RPTSandInspectionServiceObj.DeleteSandInspection(deleteEntity);

            BindGridView();
        }

        protected void gvRPTSandInspectionList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvRPTSandInspectionList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRPTSandInspectionList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
